//
//  CYRechargeViewController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/5.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYRechargeViewController.h"
#import "CYTransferViewController.h"
#import "CYRechargeHeaderView.h"
#import "CYMoneySeg.h"
#import "CYPayWayBtn.h"
@interface CYRechargeViewController ()

@property (nonatomic, strong) CYRechargeHeaderView *headerView;
@property (nonatomic, strong) CYMoneySeg *moneyseg;
@property (nonatomic, strong) CYButton *rechargeBtn; //充值按钮
@property (nonatomic, strong) CYLabel *pay_wayLBL;   //支付方式
@property (nonatomic, assign) NSInteger currentPayWayIndex;//当前选中充值方式的坐标
@property (nonatomic, strong) NSDictionary *personal_data;//个人中心信息
@property (nonatomic, strong) NSString *personal_plistName;//个人信息plist名称
@property (nonatomic, copy) NSString *money;//充值金额
@end

@implementation CYRechargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _personal_plistName = @"personal";
    self.headerView.hidden = self.moneyseg.hidden =  self.rechargeBtn.hidden = self.pay_wayLBL.hidden = NO;
    
    [self setupPayList];
    // Do any additional setup after loading the view.
}

/*headerview*/
- (CYRechargeHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[CYRechargeHeaderView alloc] init];
        [self.view addSubview:_headerView];
        
        [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(NAVIBAR_HEIGHT + STATUS_HEIGHT, 0, SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT - AL(PT(95)), 0));
        }];
    }
    return _headerView;
}

- (CYMoneySeg *)moneyseg{
    if (!_moneyseg) {
        WS(self)
        _moneyseg = [[CYMoneySeg alloc]initWithBlock:^(NSString * number) {
            weakSelf.money = number;
        }];
        _moneyseg.showLine = YES;
        [self.view addSubview:_moneyseg];
        
        [_moneyseg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(AL(PT(95)) + NAVIBAR_HEIGHT + STATUS_HEIGHT);
            make.centerX.equalTo(self.view);
            make.size.mas_equalTo(_moneyseg.bounds.size);
        }];
    }
    return _moneyseg;
}

- (CYButton *)rechargeBtn{
    if (!_rechargeBtn) {
        _rechargeBtn = [[CYButton alloc]initWithTitle:@"立即充值" titleColor:[UIColor whiteColor] backgroundColor:THEME_COLOR cornerRadius:AL(5.0)];
        [_rechargeBtn addTarget:self action:@selector(actionForRechargeBtn) forControlEvents:UIControlEventTouchUpInside];
        _rechargeBtn.titleLabel.font = [UIFont systemFontOfSize:AL(PT(40))];
        [self.view addSubview:_rechargeBtn];
        
        [_rechargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view.mas_bottom).with.offset(- AL(PT(65)));
            make.centerX.equalTo(self.view);
            make.size.mas_equalTo(CGSizeMake(AL(PT(460)), AL(PT(84))));
        }];
    }
    return _rechargeBtn;
}

- (CYLabel *)pay_wayLBL{
    if (!_pay_wayLBL) {
        _pay_wayLBL = [[CYLabel alloc]initWithText:@"充值方式" size:AL(PT(28)) textcolor:ColorHex(0X888888)];
        [self.view addSubview:_pay_wayLBL];
        
        [_pay_wayLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).with.offset(AL(PT(25)));
            make.top.equalTo(_moneyseg.mas_bottom).with.offset(AL(PT(25)));
            make.size.mas_equalTo(_pay_wayLBL.bounds.size);
        }];
    }
    return _pay_wayLBL;
}

- (void)setPersonal_data:(NSDictionary *)personal_data{
    if (!personal_data)
        _personal_data = [CYCache getCache:_personal_plistName];
    else if (_personal_data != personal_data)
        _personal_data = personal_data;
    [CYCache cache:_personal_data plist:_personal_plistName];
    self.headerView.imageName = @"portrait";
    self.headerView.userName =[CYCache readDataWithKey:@"username"];
    self.headerView.money = [_personal_data[@"coin"] floatValue];//余额
    
}
- (void)setupPayList{
    _currentPayWayIndex = -1;
    NSArray *array = @[@[@{@"title":@"网银支付",@"image":@"unionPay"},@{@"title":@"银行转账",@"image":@"transfer"}],@[@{@"title":@"微信",@"image":@"WeChat"},@{@"title":@"支付宝",@"image":@"Alipay"}]];
    for (int i = 0; i < 2; i ++) {
        for (int j = 0; j < 2 ; j ++) {
            CYPayWayBtn *payBtn = [[CYPayWayBtn alloc]initWithTitle:array[i][j][@"title"] titleColor:ColorHex(0x333333) titleSize:AL(PT(30)) Image:array[i][j][@"image"]];
            [payBtn addTarget:self action:@selector(actionForPayWay:) forControlEvents:UIControlEventTouchUpInside];
            payBtn.tag = (i == 0)?(200 + j):(202 + j);
            [self.view addSubview:payBtn];
            
            [payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_pay_wayLBL.mas_bottom).with.offset(AL(PT(80)) * j);
                make.centerX.equalTo(self.view);
                make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - AL(PT(30 * 2)), AL(PT(80))));
            }];
            
            //添加查看全部按钮
            if (i == 1 && j == 1) {
                
                CYButton *button = [[CYButton alloc]initWithTitle:@"查看全部" titleColor:TEXT_GRAY_COLOR titleSize:AL(PT(26)) Image:@"xiala"];
                [button setTitle:@"收起" forState:UIControlStateSelected];
                button.backgroundColor = [UIColor yellowColor];
                button.titleLabel.backgroundColor = THEME_COLOR;
                button.imageView.backgroundColor = BLUE_COLOR;
                [button addTarget:self action:@selector(actionForAll:) forControlEvents:UIControlEventTouchUpInside];
                
                button.titleEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, button.imageView.bounds.size.width);
                button.imageEdgeInsets = UIEdgeInsetsMake(0.0, button.titleLabel.bounds.size.width, 0.0, 0.0);
                CGSize size = [@"查看全部" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:AL(PT(26))]}];
                DeLog(@"size %f  %f ",size.width, size.height);
                [self.view addSubview:button];
                
                [button mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(payBtn.mas_bottom).with.offset(0);
                    make.centerX.equalTo(self.view);
                    make.size.mas_equalTo(CGSizeMake(button.titleLabel.bounds.size.width + button.imageView.bounds.size.width, button.titleLabel.bounds.size.height + button.imageView.bounds.size.height));
                    
                }];
            }
        }
        
    }
}


//支付方式方法
- (void)actionForPayWay:(CYPayWayBtn *)sender{
    sender.selected = !sender.selected;
    _currentPayWayIndex = (sender.selected)?sender.tag:-1;
    for (CYView *view in [self.view subviews]) {
        if ([view isKindOfClass:[CYPayWayBtn class]] && view.tag != sender.tag){
            CYPayWayBtn *new_sender = (CYPayWayBtn *)view;
            new_sender.selected = NO;
        }
    }
}
//*显示全部支付方式*/
- (void)actionForAll:(CYButton *)sender{
    
    sender.selected = !sender.selected;
    for (CYView *view in [self.view subviews]) {//便利scrollview所有子视图
        if ([view isKindOfClass:[CYPayWayBtn class]] && view.tag < 202){//找出 支付方式 按钮  并且找出隐藏的两个支付方式 按钮
            CYPayWayBtn *new_sender = (CYPayWayBtn *)view;
            if (sender.selected)
                [UIView animateWithDuration:0.3 animations:^{
                    new_sender.transform = CGAffineTransformMakeTranslation(0, AL(PT(80 * 2)));
                    sender.transform = CGAffineTransformMakeTranslation(0, AL(PT(80 * 2)));
                }];
            else
                [UIView animateWithDuration:0.3 animations:^{
                    new_sender.transform = CGAffineTransformIdentity;
                    sender.transform = CGAffineTransformIdentity;
                }];
        }
    }
    
    //查看全部 和收起切换时候，改变按钮文字与图片的相对位置
    if (sender.selected) {
        sender.titleEdgeInsets = UIEdgeInsetsMake(0.0, -sender.imageView.bounds.size.width * 1, 0.0, 0.0);
        sender.imageEdgeInsets = UIEdgeInsetsMake(0.0, 0.0,0.0, -sender.titleLabel.bounds.size.width * 1);
    }
    else {
        sender.titleEdgeInsets = UIEdgeInsetsMake(0.0, -sender.imageView.bounds.size.width * 2, 0.0, 0.0);
        sender.imageEdgeInsets = UIEdgeInsetsMake(0.0, 0.0,0.0, -sender.titleLabel.bounds.size.width * 2);
    }
}

//立即充值

- (void)actionForRechargeBtn{
    if (_money.length <= 0){
        [CYHUD show:@"请选择充值金额"];
        return;
    }
    else if (_currentPayWayIndex == 200) {//网银支付
        DeLog(@"银行转账");
        return;
    }
    else if (_currentPayWayIndex == 201){//银行转账
        CYTransferViewController *transferVC = [[CYTransferViewController alloc]init];
        transferVC.money = _money;
        [self.navigationController pushViewController:transferVC animated:YES];
        return;
    }
    else if (_currentPayWayIndex == 202){//微信支付
        DeLog(@"微信支付");
        return;
        
    }
    else if (_currentPayWayIndex == 203){//支付宝支付
        DeLog(@"支付宝支付");
        return;
    }
    else{
        [CYHUD show:@"请选择充值方式"];
    }
    
}
////网络请求
- (void)https{
    
    //个人中心请求
    NSDictionary *dict = @{@"username":[CYCache readDataWithKey:@"username"],@"token":[CYCache readDataWithKey:@"token"]};
    self.task = [CYHttps POST:API_personal parameters:dict showHUD:NO progress:nil success:^(id data) {
        if (data)
            self.personal_data = data;
        else {
            Class vc  = NSClassFromString(@"CYLoginViewController");
            [self.navigationController pushViewController:[vc new] animated:YES];
        }
    } failure:^(id string) {
        self.personal_data = nil;
    }];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self https];
    
}
- (void)setupNavigationBarTintColor:(UIColor *)color{
    [super setupNavigationBarTintColor:color];
    self.navigationItem.title = @"充值";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
